package com.superb.common.database.mybatis.expression;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;

import java.util.List;

/**
 * 重写列表 表达式
 *  自带表达式
 * @Author: ajie
 * @CreateTime: 2024-07-15 13:53
 */
@NoArgsConstructor
@AllArgsConstructor
public class SuperbExpressionList extends ExpressionList<Expression> {

    /**
     * 是否追加括号
     */
    @Setter
    private boolean brackets = true;

    /**
     * 构造器设置条件
     * @param expressionList
     */
    public SuperbExpressionList(List<String> expressionList) {
        for (String expression : expressionList) {
            super.addExpression(new StringValue(expression));
        }
    }

    @Override
    public StringBuilder appendTo(StringBuilder builder) {
        if (brackets) {
            builder.append("(");
            for (int i = 0; i < size(); i++) {
                if (i > 0) {
                    builder.append(", ");
                }
                builder.append(get(i));
            }
            return builder.append(")");
        }
        return super.appendTo(builder);
    }

}
